#include "Game.h"
#include "LuaFunctions.h"
#include "CorutinaCamara.h"
#include "CorutinaDelay.h"
#include "CorutinaPTR.h"
#include "CorutinaColor.h"
#include <iostream>



using namespace std;

int LuaFunctions::cerrar(lua_State *L)
	{
		exit(lua_tonumber(L,1));
		return 0;
	}
int LuaFunctions::camara(lua_State *L)
	{
		float x = lua_tonumber(L,1);
		float y = lua_tonumber(L,2);
		float ms = lua_tonumber(L,3);
		//cout << "x: " << x << " y: " << y << "  ms: " << ms << endl;
		g->getRutinas()->addRutina(new CorutinaCamara(x,y,ms));
		return 0;
	}

int LuaFunctions::espera( lua_State *L )
	{
		//cout << "delay: " << lua_tonumber(L,1) << endl;
		g->getRutinas()->addRutina(new CorutinaDelay(lua_tonumber(L,1)));
		return 0;
	}

int LuaFunctions::camaraToActor(lua_State *L){
	ID idActor = lua_tonumber(L,1);
	float duracion = lua_tonumber(L,2);

	Actor *a = g->getActor(idActor);
	g->getRutinas()->addRutina(new CorutinaCamara(a->getX() - g->getAnchoPantalla()/2,a->getY() - g->getAltoPantalla()/2,duracion));
	return 0;
}


int LuaFunctions::setColor(lua_State *L){
	float r = lua_tonumber(L,1);
	float gColor = lua_tonumber(L,2);
	float b = lua_tonumber(L,3);
	float duracion = lua_tonumber(L,4);
	
	g->getRutinas()->addRutina(new CorutinaColor(r,gColor,b,duracion));
	return 0;
}
int LuaFunctions::fadeOut( lua_State *L )
	{
		float duracion = lua_tonumber(L,1);
		g->getRutinas()->addRutina(new CorutinaColor(0.f,0.f,0.f,duracion));
		return 0;
	}

int LuaFunctions::fadeIn( lua_State *L )
	{
		float duracion = lua_tonumber(L,1);
		g->getRutinas()->addRutina(new CorutinaColor(1.f,1.f,1.f,duracion));
		return 0;
	}
// ==========================================================================
void LuaFunctions::registrarFunciones(lua_State *L)
	{
		lua_register(L,"cerrar",LuaFunctions::cerrar);
		lua_register(L,"moverCamara",LuaFunctions::camara);
		lua_register(L,"lockCamara",LuaFunctions::lockCamara);
		lua_register(L,"delay",LuaFunctions::espera);
		lua_register(L,"camToActor",LuaFunctions::camaraToActor);
		lua_register(L,"color",LuaFunctions::setColor);
		lua_register(L,"fadeIn",LuaFunctions::fadeIn);
		lua_register(L,"fadeOut",LuaFunctions::fadeOut);
		lua_register(L,"mover",LuaFunctions::moverActor);
	}
// funciones de ptr
int LuaFunctions::lockCamara( lua_State *L )
{
	//cout << "lock camara" << endl;
	g->getRutinas()->addRutina(new CorutinaPTR(LuaFunctions::lock));
	return 0;
}
void LuaFunctions::lock(void *data)
	{
		g->getCamara()->lock();
	}
//orden de movimiento
typedef struct _orderMoviemiento{
	_orderMoviemiento(float x1, float y1, float idActor, float dur):x(x1),y(y1),id(idActor),duracion(dur){};
	float x;
	float y;
	int id;
	float duracion;
}orderMovimiento;

int LuaFunctions::moverActor(lua_State *L){

	int actorID = lua_tonumber(L,1);
	float x		= lua_tonumber(L,2);
	float y		= lua_tonumber(L,3);
	float duracion = lua_tonumber(L,4);

	orderMovimiento *o = new orderMovimiento(x,y,actorID,duracion);

	G_RUTINAS->addRutina(new CorutinaPTR(mover,o));

	return 0;
}

void LuaFunctions::mover(void *data){
	orderMovimiento *o = static_cast<orderMovimiento*>(data);
	Actor *a = g->getActor(o->id);
		if (a)
			a->mover(o->x,o->y,o->duracion);
	delete o;
}
//=======



